from algorithm import Solution, brute_force_solution
from generate_data import generate_test_data, generate_edge_cases, generate_large_test_cases

def run_tests():
    """运行测试用例"""
    print("=" * 50)
    print("测试用例执行结果")
    print("=" * 50)
    
    # 获取所有测试用例
    basic_cases = generate_test_data()
    edge_cases = generate_edge_cases()
    large_cases = generate_large_test_cases()
    all_cases = basic_cases + edge_cases + large_cases
    
    solution = Solution()
    
    passed = 0
    failed = 0
    
    for i, (tiles, expected) in enumerate(all_cases):
        try:
            result = solution.numTilePossibilities(tiles)
            
            status = "✓" if result == expected else "✗"
            print(f"测试用例 {i+1}: {status}")
            print(f"  输入: '{tiles}'")
            print(f"  期望: {expected}")
            print(f"  实际: {result}")
            if result == expected:
                passed += 1
            else:
                failed += 1
                print(f"  错误: 期望 {expected}，实际得到 {result}")
            
            print()
            
        except Exception as e:
            print(f"测试用例 {i+1}: 执行出错")
            print(f"  输入: '{tiles}'")
            print(f"  错误: {e}")
            print()
            failed += 1
    
    # 输出测试统计
    print("=" * 50)
    print("测试统计")
    print("=" * 50)
    print(f"总测试用例数: {len(all_cases)}")
    print(f"通过: {passed}")
    print(f"失败: {failed}")
    print(f"通过率: {passed / len(all_cases) * 100:.2f}%")

def run_specific_tests():
    """运行特定测试用例"""
    print("=" * 50)
    print("特定测试用例")
    print("=" * 50)
    
    # 题目给出的示例
    test_cases = [
        ("AAB", 8),
        ("AAABBC", 188),
        ("V", 1)
    ]
    
    solution = Solution()
    
    for i, (tiles, expected) in enumerate(test_cases):
        result = solution.numTilePossibilities(tiles)
        status = "✓" if result == expected else "✗"
        
        print(f"示例 {i+1}: {status}")
        print(f"  输入: '{tiles}'")
        print(f"  期望: {expected}")
        print(f"  实际: {result}")
        
        if result == expected:
            print(f"  解释: 可能的序列为 {get_all_sequences(tiles)}")
        else:
            print(f"  错误: 期望 {expected}，实际得到 {result}")
        print()

def get_all_sequences(tiles):
    """获取所有可能的序列（用于解释）"""
    from itertools import permutations
    
    if not tiles:
        return []
    
    result = set()
    for length in range(1, len(tiles) + 1):
        for perm in permutations(tiles, length):
            result.add(''.join(perm))
    
    return sorted(list(result))

def save_test_results():
    """保存测试结果到文件"""
    print("保存测试结果...")
    
    # 获取所有测试用例
    basic_cases = generate_test_data()
    edge_cases = generate_edge_cases()
    large_cases = generate_large_test_cases()
    all_cases = basic_cases + edge_cases + large_cases
    
    solution = Solution()
    
    with open("test_results.txt", "w", encoding="utf-8") as f:
        f.write("测试结果报告\n")
        f.write("=" * 50 + "\n\n")
        
        passed = 0
        failed = 0
        
        for i, (tiles, expected) in enumerate(all_cases):
            try:
                result = solution.numTilePossibilities(tiles)
                
                f.write(f"测试用例 {i+1}:\n")
                f.write(f"  输入: '{tiles}'\n")
                f.write(f"  期望: {expected}\n")
                f.write(f"  实际: {result}\n")
                if result == expected:
                    f.write(f"  状态: 通过 ✓\n")
                    passed += 1
                else:
                    f.write(f"  状态: 失败 ✗\n")
                    f.write(f"  错误: 期望 {expected}，实际得到 {result}\n")
                    failed += 1
                
                f.write("\n")
                
            except Exception as e:
                f.write(f"测试用例 {i+1}:\n")
                f.write(f"  输入: '{tiles}'\n")
                f.write(f"  状态: 执行出错\n")
                f.write(f"  错误: {e}\n\n")
                failed += 1
        
        # 写入统计信息
        f.write("=" * 50 + "\n")
        f.write("测试统计\n")
        f.write("=" * 50 + "\n")
        f.write(f"总测试用例数: {len(all_cases)}\n")
        f.write(f"通过: {passed}\n")
        f.write(f"失败: {failed}\n")
        f.write(f"通过率: {passed / len(all_cases) * 100:.2f}%\n")
    
    print("测试结果已保存到 test_results.txt")

if __name__ == "__main__":
    # 运行特定测试用例
    run_specific_tests()
    
    # 运行完整测试
    run_tests()
    
    # 保存测试结果
    save_test_results() 